package com.elephantgreen.common;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * @author ABC
 * 
 */
public class ChequeEntry extends ElephantgreenCommon {

	private Sale sale;
	private Client client;
	private ChequeDetails cheque;

	/**
	 * Create new check entry with client and sale references
	 * 
	 * @param client
	 * @param sale
	 * @param cheque
	 */
	public ChequeEntry(Client client, Sale sale, ChequeDetails cheque) {
		super();
		this.sale = sale;
		this.client = client;
		this.cheque = cheque;
	}

	/**
	 * Create new check entry with only client reference
	 * 
	 * @param client
	 * @param cheque
	 */
	public ChequeEntry(Client client, ChequeDetails cheque) {
		super();
		this.client = client;
		this.cheque = cheque;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.elephantgreen.common.EgCommon#createXml()
	 */
	@Override
	public Document createXml() {

		Document doc = null;

		try {
			DocumentBuilderFactory docFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
			doc = docBuilder.newDocument();

			// root element
			Element rootElement = doc.createElement("chequePayments");
			doc.appendChild(rootElement);

			// chequeId element
			Element chequeElement = doc.createElement("cheque");
			rootElement.appendChild(chequeElement);

			// chequeSales element
			Element chequeSales = doc.createElement("chequeSales");
			rootElement.appendChild(chequeSales);

			// clientId element
			Element clientId = doc.createElement("clientId");
			clientId.appendChild(doc.createTextNode(getClient().getClientId()));
			chequeElement.appendChild(clientId);

			// chequeNumber element
			Element chequeNumber = doc.createElement("chequeNumber");
			chequeNumber.appendChild(doc.createTextNode(getCheque()
					.getChequeNumber()));
			chequeElement.appendChild(chequeNumber);

			// chequeAmount element
			Element chequeAmount = doc.createElement("chequeAmount");
			chequeAmount.appendChild(doc.createTextNode(getCheque()
					.getChequeAmount()));
			chequeElement.appendChild(chequeAmount);

			// bankName element
			Element bankName = doc.createElement("bankName");
			bankName.appendChild(doc.createTextNode(getCheque().getBankName()));
			chequeElement.appendChild(bankName);

			// branchName element
			Element branchName = doc.createElement("branchName");
			branchName.appendChild(doc.createTextNode(getCheque()
					.getBranchName()));
			chequeElement.appendChild(branchName);

			// dueDate element
			Element dueDate = doc.createElement("dueDate");
			dueDate.appendChild(doc.createTextNode(getCheque().getDueDate()));
			chequeElement.appendChild(dueDate);

			// chequeStatus element
			Element chequeStatus = doc.createElement("chequeStatus");
			chequeStatus.appendChild(doc.createTextNode(getCheque()
					.getChequeStatus()));
			chequeElement.appendChild(chequeStatus);

			// saleId element
			Element invoiceNumber = doc.createElement("invoiceNumber");
			invoiceNumber.appendChild(doc.createTextNode(getSale()
					.getInvoiceNumber()));
			chequeSales.appendChild(invoiceNumber);

		} catch (ParserConfigurationException pce) {
			pce.printStackTrace();
		}

		return doc;

	}

	public Sale getSale() {
		return sale;
	}

	public void setSale(Sale sale) {
		this.sale = sale;
	}

	public Client getClient() {
		return client;
	}

	public void setClient(Client client) {
		this.client = client;
	}

	public ChequeDetails getCheque() {
		return cheque;
	}

	public void setCheque(ChequeDetails cheque) {
		this.cheque = cheque;
	}

}
